package com.zzj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzj.bean.TabFlower;
import com.zzj.bean.TabOrder;
import com.zzj.dao.SonOrderMapper;
import com.zzj.dao.TabFlowerMapper;
import com.zzj.dao.TabOrderMapper;
import com.zzj.service.TabOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * InnoDB free: 8192 kB 服务实现类
 * </p>
 *
 * @author 花
 * @since 2022-03-04
 */
@Service
public class TabOrderServiceImpl extends ServiceImpl<TabOrderMapper, TabOrder> implements TabOrderService {

    @Resource
    private TabOrderMapper orderMapper;
    @Resource
    private TabFlowerMapper flowerMapper;
    @Resource
    private SonOrderMapper sonOrderMapper;

    @Override
    public PageInfo<TabOrder> getAllOrders(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TabOrder> orders = orderMapper.getAllOrders();
        return new PageInfo<>(orders);
    }

    @Override
    public boolean addOrder(TabOrder order) {
        order.setOtime(new Date());
        order.setOstatus("已完成");
        order.setOoo("1");
        TabFlower flower = flowerMapper.selectById(order.getSonOrders().get(0).getFid());
        double v = flower.getPrice() * order.getSonOrders().get(0).getScount();
        order.getSonOrders().get(0).setMoney(v);
        order.setOtotal(v);
        order.getSonOrders().get(0).setSstatus("已完成");
        int i1 = orderMapper.insert(order);
        order.getSonOrders().get(0).setOid(order.getOid());
        int i2 = sonOrderMapper.insert(order.getSonOrders().get(0));
        return i1==1 && i2 == 1;
    }
}
